//
//  STVisitorView.swift
//  STVVeibo
//
//  Created by leergou on 16/7/19.
//  Copyright © 2016年 WhiteHouse. All rights reserved.
//

import UIKit
import SnapKit

class STVisitorView: UIView {

    // 定义闭包属性,用于按钮点击回传
    var closure:(()->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        // 设置界面
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 监听方法
    @objc private func loginClick(){
        
        closure?()
        
    }
    
    //MARK: - 提供一个方法,供不同页面修改界面信息
    func setupVisitorView(title: String?, imgName: String?) {
        
        if let tit = title, img = imgName {
            // 不是 '首页'
            iconImageView.image = UIImage(named: img)
            feedImageView.hidden = true
            titleLabel.text = tit
        } else {
            // 是'首页'
            setAnimation()
        }
        
        
    }
    
    //MARK: - 设置圆圈动画
    func setAnimation() {
        
        let basicAnimation = CABasicAnimation(keyPath: "transform.rotation")
        basicAnimation.duration = 10
        basicAnimation.toValue = M_PI * 2
        basicAnimation.repeatCount = MAXFLOAT
        basicAnimation.removedOnCompletion = false
        // 添加动画
        feedImageView.layer.addAnimation(basicAnimation, forKey: nil)
    }
    
    
    
    //MARK: - setupUI
    func setupUI() {
    
        backgroundColor = RGB(237, green: 237, blue: 237)
        // 01添加控件
        addSubview(feedImageView)
        addSubview(maskImageView)
        addSubview(iconImageView)
        addSubview(titleLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        
        // 02添加约束
        feedImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        titleLabel.snp_makeConstraints { (make) in
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.centerX.equalTo(self)
            make.size.equalTo(CGSize(width: 230, height: 36))
        }
        loginButton.snp_makeConstraints { (make) in
            // 记得设置一下图片的拉伸方式,否则效果不对
            make.top.equalTo(titleLabel.snp_bottom).offset(16)
            make.leading.equalTo(titleLabel)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        registerButton.snp_makeConstraints { (make) in
            make.top.equalTo(loginButton)
            make.size.equalTo(loginButton)
            make.trailing.equalTo(titleLabel)
        }
        
    }
    
    
    //MARK: 懒加载控件
    // 圆圈
    private lazy var feedImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_image_smallicon")
    // 房子
    private lazy var iconImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_image_house")
    // 遮盖
    private lazy var maskImageView: UIImageView = UIImageView(imgName: "visitordiscover_feed_mask_smallicon")
    
    // 文字
    private lazy var titleLabel: UILabel = {
        let label = UILabel(font: 14, textColor: UIColor.grayColor())
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        label.textAlignment = NSTextAlignment.Center
        label.numberOfLines = 0
        
        return label
    }()
    
    // 登录按钮
    private lazy var loginButton: UIButton = {
        
        let button = UIButton()
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        return button
    }()
    
    // 注册按钮
    private lazy var registerButton: UIButton = {
        
        let button = UIButton()
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        return button
    }()

    
}











